﻿#include "lv_time_date.h"
#include "lv_main_page.h"
#include <stdio.h>  
#include <stdlib.h>

static void system_date_timer(lv_timer_t* timer);
static int  is_leap_year(int year);
static int  get_days_in_month(int year, int month);
static void event_handler(lv_event_t* e);
static void Date_or_Time_set_btn_event_handler(lv_event_t* e);
static void curent_date_set(void);
static void btn_yes_or_no_event_handler(lv_event_t* e);
static void Date_or_Time_set_pannel(void);//日期和时间设置面板
static void testbtn_event_handler(lv_event_t* e);
static void split_string(const char* str, char output[][PAIR_LEN + 1], int num_pairs);
static void _ui_flag_modify(lv_obj_t* target, int32_t flag, int value);
static void ui_event_date_roll(lv_event_t* e);
static void ui_event_time_roll(lv_event_t* e);
Date        current_date = { 2024, 10, 31 ,2024, 10, 31 }; // 全局日期变量，避免内存泄漏 年选项号=year_history-2020;月选项号=month_history-1;日选项号=day_history-1
Time        current_time = { 12, 30, 0, 12, 30, 0 , 0 }; // 全局时间变量，避免内存泄漏
char        pairs_YMD[NUM_PAIRS][PAIR_LEN + 1];//临时存储年月日的分解字符数组
char        pairs_HMS[NUM_PAIRS - 1][PAIR_LEN + 1];//临时存储时分秒的分解字符数组
uint8_t   flag_24_yes = 0;/**24小时制还是12小时制*/
const char* year_history = { "2024"};  // 年份历史记录
static const char AM_PM[]={"AM\nPM"};//上下午字符串
static const char date_28[] = {"1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n25\n26\n27\n28" };/*28天字符串*/
static const char date_29[] ={"1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n25\n26\n27\n28\n29"};/*29天字符串*/
static const char date_30[] = {"1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n25\n26\n27\n28\n29\n30"};/*30天字符串*/
static const char date_31[] = {"1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n25\n26\n27\n28\n29\n30\n31"};/*31天字符串*/
static const char year_str[] = {"2020\n2021\n2022\n2023\n2024\n2025\n2026\n2027\n2028\n2029\n2030\n2031\n2032\n2033\n2034\n2035\n2036\n2037\n2038\n2039\n2040\n2041\n2042\n2043\n2044\n2045\n2046\n2047\n2048\n2049\n2050\n2051\n2052\n2053\n2054"};/*年份字符串*/
static const char month_str[] = {"1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12"};/*月份字符串*/
static const char hour_str_12[]={"1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12" };//12小时制时字符串
static const char hour_str_24[]={"0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23"};//24小时制时字符串
static const char minute_str[] = {"0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n25\n26\n27\n28\n29\n30\n31\n32\n33\n34\n35\n36\n37\n38\n39\n40\n41\n42\n43\n44\n45\n46\n47\n48\n49\n50\n51\n52\n53\n54\n55\n56\n57\n58\n59"};//分钟字符串
static const char second_str[]=  {"0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n25\n26\n27\n28\n29\n30\n31\n32\n33\n34\n35\n36\n37\n38\n39\n40\n41\n42\n43\n44\n45\n46\n47\n48\n49\n50\n51\n52\n53\n54\n55\n56\n57\n58\n59"};//秒字符串

/**
 * @brief 日期时间显示和设置函数
 * @param 无
 * @return 无 
 */
void lv_timer(void) {
    lv_timer_t* timer = lv_timer_create(system_date_timer, 1000, &current_time);//一秒钟触发一次定时时间
    //定义一个标签用于显示时间
    realtime1_label = lv_label_create(main_page);
    lv_obj_set_size(realtime1_label, 80, 70);//尺寸
    lv_obj_set_align(realtime1_label, LV_ALIGN_TOP_RIGHT);//右上对齐
    lv_obj_set_pos(realtime1_label, -10, 0);//右偏移30

    //定义一个标签用于显示日期
    date1_label = lv_label_create(main_page);
    lv_obj_set_size(date1_label, 80, 50);//尺寸
    lv_obj_set_align(date1_label, LV_ALIGN_TOP_RIGHT);//右上对齐
    lv_obj_set_pos(date1_label, -100, 0);//
    if (flag_24_yes) {
        lv_label_set_text_fmt(realtime1_label, "%02d:%02d:%02d", current_time.hour, current_time.min, current_time.sec);
    }
    else {
        if (current_time.Am0_Pm1 == 0) {
            lv_label_set_text_fmt(realtime1_label, "%02d:%02d:%02d AM", current_time.hour, current_time.min, current_time.sec);
        }
        else if (current_time.Am0_Pm1==1){   
            lv_label_set_text_fmt(realtime1_label, "%02d:%02d:%02d PM", current_time.hour, current_time.min, current_time.sec);
        }
    }
    lv_label_set_text_fmt(date1_label,"%d%-d%-%d",current_date.year, current_date.month, current_date.day);//设置日期显示格式
    curent_date_set();
}
/*
1 秒定时器触发回调函数
 */
static void system_date_timer(lv_timer_t* timer) {
    // 更新秒  
    current_time.sec++;
    if (current_time.sec >= 60) {
        current_time.sec = 0;
        // 更新分  
        current_time.min++;
        if (current_time.min >= 60) {
            current_time.min = 0;
            // 更新时并检查日期更新  
            current_time.hour++;
            if (flag_24_yes){//24小时制
                if (current_time.hour >= 24) {
                    current_time.hour = 0;
                    // 判断日期是否为每月的最后一天
                    // 更新日期并检查是否为每年的最后一天
                    current_date.day++;
                    if (is_leap_year(current_date.year) && current_date.month == 2 && current_date.day > 29) {
                        // 闰年2月后的日期更新逻辑（这里简化，只考虑2月29日后回到3月1日）  
                        current_date.month = 3;
                        current_date.day = 1;
                    }
                    else {
                        // 非闰年或非2月后的日期更新逻辑  
                        if (current_date.day > get_days_in_month(current_date.year, current_date.month)) {
                            current_date.day = 1;
                            current_date.month++;
                            
                            if (current_date.month > 12) {
                                current_date.month = 1;
                                current_date.year++;
                                
                            }
                        }
                    }
                }
            }
            else {//12小时制
                if (current_time.hour >= 12) {
                    current_time.hour = 0;
                }
                if (current_time.hour == 0) {
                    current_time.hour = 12;
                }
                if (current_time.Am0_Pm1 == 1) {//如果是下午，则跟新日期
                    current_date.day++;// 更新日期
                }
                if (is_leap_year(current_date.year) && current_date.month == 2 && current_date.day > 29) {
                    // 闰年2月后的日期更新逻辑（这里简化，只考虑2月29日后回到3月1日）  
                    current_date.month = 3;
                    current_date.day = 1;
                }
                else {
                    // 非闰年或非2月后的日期更新逻辑  
                    if (current_date.day > get_days_in_month(current_date.year, current_date.month)) {
                        current_date.day = 1;
                        current_date.month++;// 更新月份
                        
                        if (current_date.month > 12) {
                            current_date.month = 1;
                            current_date.year++;// 更新年份  
                        }
                    }
                }
            }
        }
    }

    // 更新标签文本  

    if (flag_24_yes) {
        lv_label_set_text_fmt(realtime1_label, "%02d:%02d:%02d", current_time.hour, current_time.min, current_time.sec);
    }
    else {
        if (current_time.Am0_Pm1 == 0) {
            lv_label_set_text_fmt(realtime1_label, "%02d:%02d:%02d AM", current_time.hour, current_time.min, current_time.sec);
        }
        else if (current_time.Am0_Pm1 == 1) {
            lv_label_set_text_fmt(realtime1_label, "%02d:%02d:%02d PM", current_time.hour, current_time.min, current_time.sec);
        }
    }
    lv_label_set_text_fmt(date1_label, "%d-%02d-%02d", current_date.year, current_date.month, current_date.day);//刷新显示
}
// 辅助函数：判断是否为闰年  
static int is_leap_year(int year) {
    return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
// 辅助函数：获取某年某月的天数  
static int get_days_in_month(int year, int month) {
    static const int days_in_month[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    if (month == 2 && is_leap_year(year)) {
        return 29;
    }
    return days_in_month[month - 1];//返回某月的天数
}
//日期设置按钮函数
void curent_date_set(void) {
    // 创建一个按钮  
    Date_Time_set_btn = lv_btn_create(main_page);
    lv_obj_set_size(Date_Time_set_btn, 150, 40);
    lv_obj_align(Date_Time_set_btn, LV_ALIGN_TOP_LEFT,10,20);//设置位置
    
    lv_obj_set_style_bg_color(Date_Time_set_btn, lv_color_hex(0x007DFF), LV_STATE_DEFAULT);//设置默认状态为蓝色
    lv_obj_set_style_bg_color(Date_Time_set_btn, lv_color_hex(0x0050A0), LV_STATE_PRESSED);//设置按下状态为淡蓝色
    lv_obj_add_event_cb(Date_Time_set_btn, Date_or_Time_set_btn_event_handler, LV_EVENT_CLICKED, NULL);
    lv_obj_t* label = lv_label_create(Date_Time_set_btn);
    lv_label_set_text(label, "Set Date & Time");
}
//日期设置按钮回调函数
static void Date_or_Time_set_btn_event_handler(lv_event_t* e) {
    lv_obj_t* btn = lv_event_get_target(e);
    lv_obj_clear_flag(btn, LV_OBJ_FLAG_CLICKABLE);//禁止再次点击
    lv_obj_add_state(btn, LV_STATE_PRESSED);//保持按钮为按下状态
    lv_obj_clear_state(btn, LV_STATE_DEFAULT);//清除默认状态
    Date_or_Time_set_pannel();//日期和时间设置面板

}

/*

 * 日期和时间设置面板

 */
static void Date_or_Time_set_pannel(void)
{
    currentdate_panle = lv_obj_create(main_page);
    lv_obj_set_size(currentdate_panle, 400, 400);
    lv_obj_set_align(currentdate_panle, LV_ALIGN_CENTER);//设置对齐方式为中间对齐
    lv_obj_set_style_bg_color(currentdate_panle, lv_color_hex(0xFFFFFF), LV_PART_MAIN);//设置背景颜色为白色
    lv_obj_set_style_bg_opa(currentdate_panle, 20, LV_PART_MAIN);//设置背景透明度为255%
    lv_obj_set_style_border_width(currentdate_panle, 1, LV_PART_MAIN);//设置边框宽度为1像素
    lv_obj_set_style_border_color(currentdate_panle, lv_color_hex(0x000000), LV_PART_MAIN);//设置边框颜色为黑色
    lv_obj_set_style_shadow_width(currentdate_panle, 10, LV_PART_MAIN);//设置阴影宽度为10像素
    lv_obj_set_style_shadow_color(currentdate_panle, lv_color_hex(0x000000), LV_PART_MAIN);//设置阴影颜色为黑色
    lv_obj_set_style_shadow_ofs_x(currentdate_panle, 5, LV_PART_MAIN);//设置阴影偏移量x轴方向为5像素
    lv_obj_set_style_shadow_ofs_y(currentdate_panle, 5, LV_PART_MAIN);//设置阴影偏移量y轴方向为5像素
    lv_obj_set_style_shadow_spread(currentdate_panle, 0, LV_PART_MAIN);//设置阴影扩散度为0
    lv_obj_set_style_shadow_opa(currentdate_panle, 100, LV_PART_MAIN);//设置阴影透明度为100%
    lv_obj_set_style_radius(currentdate_panle, 10, LV_PART_MAIN);//设置圆角半径为10像素
    lv_obj_set_style_clip_corner(currentdate_panle, true, LV_PART_MAIN);//设置裁剪角点

    ui_year_roll = lv_roller_create(currentdate_panle);
    lv_roller_set_options(ui_year_roll,year_str ,LV_ROLLER_MODE_INFINITE);
    lv_roller_set_selected(ui_year_roll, current_date.year-2020, LV_ANIM_OFF);
    lv_obj_set_size(ui_year_roll, 85, 100);
    lv_obj_align(ui_year_roll, LV_ALIGN_TOP_LEFT, 10, 20);
    lv_obj_set_style_text_letter_space(ui_year_roll, 2, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_line_space(ui_year_roll, 16, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_align(ui_year_roll, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_border_width(ui_year_roll, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_color(ui_year_roll, lv_color_hex(0x001CFF), LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_text_opa(ui_year_roll, 255, LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_text_letter_space(ui_year_roll, 2, LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_text_line_space(ui_year_roll, 16, LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_text_align(ui_year_roll, LV_TEXT_ALIGN_CENTER, LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui_year_roll, &lv_font_montserrat_20, LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_radius(ui_year_roll, 0, LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(ui_year_roll, lv_color_hex(0xC8C0C0), LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_opa(ui_year_roll, 255, LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_opa(ui_year_roll, 255, LV_PART_SELECTED | LV_STATE_DEFAULT);

    ui_month_roll = lv_roller_create(currentdate_panle);
    lv_roller_set_options(ui_month_roll, month_str,LV_ROLLER_MODE_NORMAL);
    lv_roller_set_selected(ui_month_roll, current_date.month-1, LV_ANIM_OFF);
    lv_obj_set_size(ui_month_roll, 85, 100);
    lv_obj_align(ui_month_roll, LV_ALIGN_TOP_LEFT, 95, 20);
    lv_obj_set_style_text_letter_space(ui_month_roll, 2, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_line_space(ui_month_roll, 16, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_border_width(ui_month_roll, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_color(ui_month_roll, lv_color_hex(0x001CFF), LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_text_opa(ui_month_roll, 255, LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_text_letter_space(ui_month_roll, 2, LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_text_line_space(ui_month_roll, 16, LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_text_align(ui_month_roll, LV_TEXT_ALIGN_CENTER, LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui_month_roll, &lv_font_montserrat_20, LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_radius(ui_month_roll, 0, LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(ui_month_roll, lv_color_hex(0xC8C0C0), LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_opa(ui_month_roll, 255, LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_opa(ui_month_roll, 255, LV_PART_SELECTED | LV_STATE_DEFAULT);

    ui_day_roll = lv_roller_create(currentdate_panle);
    lv_roller_set_options(ui_day_roll,date_31,LV_ROLLER_MODE_INFINITE);
    lv_roller_set_selected(ui_day_roll, current_date.day-1, LV_ANIM_OFF);
    lv_obj_set_size(ui_day_roll, 85, 100);
    lv_obj_align(ui_day_roll, LV_ALIGN_TOP_LEFT, 180, 20);
    lv_obj_set_style_text_letter_space(ui_day_roll, 2, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_line_space(ui_day_roll, 16, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_border_width(ui_day_roll, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_color(ui_day_roll, lv_color_hex(0x001CFF), LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_text_opa(ui_day_roll, 255, LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_text_letter_space(ui_day_roll, 2, LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_text_line_space(ui_day_roll, 16, LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_text_align(ui_day_roll, LV_TEXT_ALIGN_CENTER, LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui_day_roll, &lv_font_montserrat_20, LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_radius(ui_day_roll, 0, LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(ui_day_roll, lv_color_hex(0xC8C0C0), LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_opa(ui_day_roll, 255, LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_opa(ui_day_roll, 255, LV_PART_SELECTED | LV_STATE_DEFAULT);
//时间设置
    ui_Label_date_set = lv_label_create(currentdate_panle);
    lv_obj_set_width(ui_Label_date_set, LV_SIZE_CONTENT);   /// 1
    lv_obj_set_height(ui_Label_date_set, LV_SIZE_CONTENT);    /// 1
    lv_obj_align(ui_Label_date_set, LV_ALIGN_TOP_MID, 0, 0);
    lv_label_set_text(ui_Label_date_set, "Date set");

    ui_Switch24or12 = lv_switch_create(currentdate_panle);
    lv_obj_set_size(ui_Switch24or12, 50, 25);
    lv_obj_align(ui_Switch24or12, LV_ALIGN_CENTER, -100, -20);

    ui_Label_24H = lv_label_create(currentdate_panle);
    lv_obj_set_width(ui_Label_24H, LV_SIZE_CONTENT);   /// 1
    lv_obj_set_height(ui_Label_24H, LV_SIZE_CONTENT);    /// 1
    lv_obj_set_x(ui_Label_24H, 22);
    lv_obj_set_y(ui_Label_24H, 26);
    lv_label_set_text(ui_Label_24H, "24H");
    lv_obj_align_to(ui_Label_24H, ui_Switch24or12, LV_ALIGN_OUT_LEFT_MID, -5, 0);
    ui_AM_PMroll = lv_roller_create(currentdate_panle);
    lv_roller_set_options(ui_AM_PMroll, AM_PM, LV_ROLLER_MODE_INFINITE);
    lv_roller_set_selected(ui_AM_PMroll, 2, LV_ANIM_OFF);//默认显示PM
    lv_obj_set_size(ui_AM_PMroll, 75, 100);
    lv_obj_align(ui_AM_PMroll, LV_ALIGN_BOTTOM_LEFT,10,-75);
    lv_obj_set_style_text_letter_space(ui_AM_PMroll, 2, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_line_space(ui_AM_PMroll, 16, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_border_width(ui_AM_PMroll, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_color(ui_AM_PMroll, lv_color_hex(0x001CFF), LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_text_opa(ui_AM_PMroll, 255, LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_text_letter_space(ui_AM_PMroll, 2, LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_text_line_space(ui_AM_PMroll, 16, LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_text_align(ui_AM_PMroll, LV_TEXT_ALIGN_CENTER, LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui_AM_PMroll, &lv_font_montserrat_20, LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_radius(ui_AM_PMroll, 0, LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(ui_AM_PMroll, lv_color_hex(0xC8C0C0), LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_opa(ui_AM_PMroll, 255, LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_opa(ui_AM_PMroll, 255, LV_PART_SELECTED | LV_STATE_DEFAULT);

    ui_hour_roll = lv_roller_create(currentdate_panle);
    lv_roller_set_options(ui_hour_roll, hour_str_12, LV_ROLLER_MODE_INFINITE);
    //lv_roller_set_selected(ui_hour_roll, current_time.hour_set_value, LV_ANIM_OFF);//默认显示当前时间
    lv_obj_set_size(ui_hour_roll, 75, 100);
    lv_obj_align(ui_hour_roll, LV_ALIGN_BOTTOM_LEFT, 75, -75);
    lv_obj_set_style_text_letter_space(ui_hour_roll, 2, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_line_space(ui_hour_roll, 16, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_border_width(ui_hour_roll, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_color(ui_hour_roll, lv_color_hex(0x001CFF), LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_text_opa(ui_hour_roll, 255, LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_text_letter_space(ui_hour_roll, 2, LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_text_line_space(ui_hour_roll, 16, LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_text_align(ui_hour_roll, LV_TEXT_ALIGN_CENTER, LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui_hour_roll, &lv_font_montserrat_20, LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_radius(ui_hour_roll, 0, LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(ui_hour_roll, lv_color_hex(0xC8C0C0), LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_opa(ui_hour_roll, 255, LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_opa(ui_hour_roll, 255, LV_PART_SELECTED | LV_STATE_DEFAULT);

    ui_min_roll = lv_roller_create(currentdate_panle);
    lv_roller_set_options(ui_min_roll, minute_str,LV_ROLLER_MODE_NORMAL);
    lv_roller_set_selected(ui_min_roll, 59, LV_ANIM_OFF);//默认显示当前时间
    lv_obj_set_size(ui_min_roll, 75, 100);
    lv_obj_align(ui_min_roll, LV_ALIGN_BOTTOM_LEFT,150,-75);
    lv_obj_set_style_text_letter_space(ui_min_roll, 2, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_line_space(ui_min_roll, 16, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_border_width(ui_min_roll, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_color(ui_min_roll, lv_color_hex(0x001CFF), LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_text_opa(ui_min_roll, 255, LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_text_letter_space(ui_min_roll, 2, LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_text_line_space(ui_min_roll, 16, LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_text_align(ui_min_roll, LV_TEXT_ALIGN_CENTER, LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui_min_roll, &lv_font_montserrat_20, LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_radius(ui_min_roll, 0, LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(ui_min_roll, lv_color_hex(0xC8C0C0), LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_opa(ui_min_roll, 255, LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_opa(ui_min_roll, 255, LV_PART_SELECTED | LV_STATE_DEFAULT);

    ui_sec_roll = lv_roller_create(currentdate_panle);
    lv_roller_set_options(ui_sec_roll, second_str, LV_ROLLER_MODE_NORMAL);//选项数量超过30时，LV_ROLLER_MODE_INFINITE滚动条显示不全，需要改成LV_ROLLER_MODE_NORMAL
    lv_roller_set_selected(ui_sec_roll,55, LV_ANIM_OFF);//看是否需要实时更新
    lv_obj_set_size(ui_sec_roll, 75, 100);
    lv_obj_align(ui_sec_roll, LV_ALIGN_BOTTOM_LEFT,225,-75);
    lv_obj_set_style_text_letter_space(ui_sec_roll, 2, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_line_space(ui_sec_roll, 16, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_align(ui_sec_roll, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_border_width(ui_sec_roll, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_color(ui_sec_roll, lv_color_hex(0x001CFF), LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_text_opa(ui_sec_roll, 255, LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_text_letter_space(ui_sec_roll, 2, LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_text_line_space(ui_sec_roll, 16, LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_text_align(ui_sec_roll, LV_TEXT_ALIGN_CENTER, LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui_sec_roll, &lv_font_montserrat_20, LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_radius(ui_sec_roll, 0, LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(ui_sec_roll, lv_color_hex(0xC8C0C0), LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_opa(ui_sec_roll, 255, LV_PART_SELECTED | LV_STATE_DEFAULT);
    lv_obj_set_style_opa(ui_sec_roll, 255, LV_PART_SELECTED | LV_STATE_DEFAULT);

    ui_time_setLabel = lv_label_create(currentdate_panle);
    lv_obj_set_width(ui_time_setLabel, LV_SIZE_CONTENT);   /// 1
    lv_obj_set_height(ui_time_setLabel, LV_SIZE_CONTENT);    /// 1
    lv_obj_set_x(ui_time_setLabel, 0);
    lv_obj_set_y(ui_time_setLabel, -10);
    lv_obj_align(ui_time_setLabel, LV_ALIGN_CENTER, 0, -30);
    lv_label_set_text(ui_time_setLabel, "Time set");

    lv_obj_add_event_cb(ui_year_roll,  ui_event_date_roll, LV_EVENT_VALUE_CHANGED, ui_year_roll);
    lv_obj_add_event_cb(ui_month_roll, ui_event_date_roll, LV_EVENT_VALUE_CHANGED, ui_month_roll);
    lv_obj_add_event_cb(ui_day_roll,   ui_event_date_roll, LV_EVENT_VALUE_CHANGED, ui_day_roll);

    lv_obj_add_event_cb(ui_Switch24or12, ui_event_time_roll, LV_EVENT_VALUE_CHANGED, NULL);
    lv_obj_add_event_cb(ui_hour_roll, ui_event_time_roll, LV_EVENT_VALUE_CHANGED, ui_hour_roll);
    lv_obj_add_event_cb(ui_AM_PMroll, ui_event_time_roll, LV_EVENT_VALUE_CHANGED, ui_AM_PMroll);
    lv_obj_add_event_cb(ui_min_roll,  ui_event_time_roll, LV_EVENT_VALUE_CHANGED, ui_min_roll);
    lv_obj_add_event_cb(ui_sec_roll,  ui_event_time_roll, LV_EVENT_VALUE_CHANGED, ui_sec_roll);

    btn_yes = lv_btn_create(currentdate_panle);
    lv_obj_set_align(btn_yes, LV_ALIGN_BOTTOM_LEFT);//左下对齐
    lv_obj_set_size(btn_yes, 100, 40);
    lv_obj_set_pos(btn_yes, 0, 0);
    lv_obj_set_style_bg_color(btn_yes, lv_color_hex(0X017189), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_opa(btn_yes, 100, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_opa(btn_yes, 100, LV_PART_MAIN | LV_STATE_DEFAULT);
    label_yes = lv_label_create(btn_yes);
    lv_label_set_text(label_yes, "YES");//确定
    lv_obj_align(label_yes, LV_ALIGN_CENTER, 0, 0);
    lv_obj_add_event_cb(btn_yes, btn_yes_or_no_event_handler, LV_EVENT_CLICKED, NULL);

    btn_no = lv_btn_create(currentdate_panle);
    lv_obj_set_align(btn_no, LV_ALIGN_BOTTOM_LEFT);//左下对齐
    lv_obj_set_size(btn_no, 100, 40);
    lv_obj_set_pos(btn_no, 120, 0);
    lv_obj_set_style_bg_color(btn_no, lv_color_hex(0X017189), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_opa(btn_no, 100, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_opa(btn_no, 100, LV_PART_MAIN | LV_STATE_DEFAULT);
    label_no = lv_label_create(btn_no);
    lv_label_set_text(label_no, "BCAK");//取消
    lv_obj_align(label_no, LV_ALIGN_CENTER, 0, 0);
    lv_obj_add_event_cb(btn_no, btn_yes_or_no_event_handler, LV_EVENT_CLICKED, NULL);
}

/**
 * @brief 日期滚动事件回调函数
 * @param e 
 */
void ui_event_date_roll(lv_event_t* e){//设置的值存入current_date的set_value数据中
    lv_obj_t* obj = lv_event_get_target(e);
    current_date.year_set_value = lv_roller_get_selected(ui_year_roll)+2020;
    current_date.month_set_value = lv_roller_get_selected(ui_month_roll)+1;
    current_date.day_set_value = lv_roller_get_selected(ui_day_roll)+1;
    uint16_t hour = lv_roller_get_selected(ui_hour_roll)+1;
    uint16_t minute = lv_roller_get_selected(ui_min_roll)+1;
    uint16_t second = lv_roller_get_selected(ui_sec_roll)+1;
    if (obj == ui_year_roll) {
        if (is_leap_year(current_date.year_set_value) ){//如果是闰年
           
            if (current_date.month_set_value == 2) { //且为2月
                //将选项改为29天
                lv_roller_set_options(ui_day_roll, date_29, LV_ROLLER_MODE_INFINITE);
            }
        }
        else {//不是闰年
            if (current_date.month_set_value == 1 || current_date.month_set_value == 3 ||
                current_date.month_set_value == 5 || current_date.month_set_value == 7 ||
                current_date.month_set_value == 8 || current_date.month_set_value == 10 ||
                current_date.month_set_value == 12) {//31天的月份
                lv_roller_set_options(ui_day_roll, date_31, LV_ROLLER_MODE_INFINITE);
            }
            else if (current_date.month_set_value == 4 || current_date.month_set_value == 6 || current_date.month_set_value == 9 || current_date.month_set_value == 11) {//30天的月份
                lv_roller_set_options(ui_day_roll, date_30, LV_ROLLER_MODE_INFINITE);
            }
            else {//28天的2月
                lv_roller_set_options(ui_day_roll, date_28, LV_ROLLER_MODE_INFINITE);
            }
        }
    }
    if (obj == ui_month_roll) {
        if (current_date.month_set_value == 2) {
            //判断是否为闰年
            if (is_leap_year(current_date.year_set_value)) {
                lv_roller_set_options(ui_day_roll, date_29, LV_ROLLER_MODE_INFINITE);
            }
            else {
                lv_roller_set_options(ui_day_roll, date_28, LV_ROLLER_MODE_INFINITE);
            }
        }
        else if (current_date.month_set_value == 4 || current_date.month_set_value == 6 || current_date.month_set_value == 9 || current_date.month_set_value == 11) {//30天的月份
            lv_roller_set_options(ui_day_roll, date_30, LV_ROLLER_MODE_INFINITE);           
        }
        else {//31天的月份
            lv_roller_set_options(ui_day_roll, date_31, LV_ROLLER_MODE_INFINITE);
        }
    }
    current_date.day_set_value = lv_roller_get_selected(ui_day_roll)+1;//获取日
    printf("option year:%d,month:%d,day:%d\n", current_date.year_set_value, current_date.month_set_value, current_date.day_set_value);
  
}
/**
 * @brief 时间滚动事件回调函数
 * @param e 
 */
void ui_event_time_roll(lv_event_t* e){
    lv_obj_t* obj = lv_event_get_target(e);
    if (obj == ui_Switch24or12) {//24小时开关有动作
        _ui_flag_modify(ui_AM_PMroll, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_TOGGLE);
        flag_24_yes = !flag_24_yes;//24小时制切换
        if (flag_24_yes) {//24小时制
            lv_roller_set_options(ui_hour_roll, hour_str_24, LV_ROLLER_MODE_INFINITE);
            current_time.hour_set_value = lv_roller_get_selected(ui_hour_roll);

            //时间由12小时制改为24小时制，要将当前时间折算到24小时制
            //0-12时，hour值不变，12-24时，hour值要加12
            if (current_time.Am0_Pm1==1){//如果是下午
                current_time.hour += 12;
            }
            else if (current_time.hour == 12&& current_time.Am0_Pm1==0) {//凌晨12点
                current_time.hour = 0;               
            }
        }
        else {//12小时制
            //时间由24小时制改为12小时制，要将当前时间折算到12小时制
            //13-23时，hour值要减12
            if (current_time.hour >= 13&& current_time.hour <= 23) {
                current_time.hour -= 12;
                current_time.Am0_Pm1 = 1;
            }
            else if (current_time.hour == 0) {
                current_time.hour = 12;
                current_time.Am0_Pm1 = 0;
            }
            lv_roller_set_options(ui_hour_roll, hour_str_12, LV_ROLLER_MODE_INFINITE);
            current_time.hour_set_value = lv_roller_get_selected(ui_hour_roll) + 1;
        }
    }
    else if (obj == ui_AM_PMroll) {//AM/PM选择器有动作
        current_time.Am0_Pm1= lv_roller_get_selected(ui_AM_PMroll);
        }
    else {//其余滑块有动作
        if (flag_24_yes) {
            current_time.hour_set_value = lv_roller_get_selected(ui_hour_roll);
        }
        else {
            current_time.hour_set_value = lv_roller_get_selected(ui_hour_roll) + 1;
        }
    }   
    current_time.min_set_value = lv_roller_get_selected(ui_min_roll);
    current_time.sec_set_value = lv_roller_get_selected(ui_sec_roll);
    printf("set hour:%02d,min:%02d,sec:%02d\n", current_time.hour_set_value, current_time.min_set_value, current_time.sec_set_value);
}
/**
 * @brief YES和NO按钮回调函数
 * @param e
 */
static void btn_yes_or_no_event_handler(lv_event_t* e) {
    //判断时间触发对象
    lv_obj_t* obj = lv_event_get_target(e);
    if (obj == btn_yes) {
        //设置日期和时间
        //获取年月日
        current_date.year = current_date.year_set_value;
        current_date.month = current_date.month_set_value;
        current_date.day = current_date.day_set_value;
        //获取时分秒
        current_time.hour = current_time.hour_set_value;
        current_time.min = current_time.min_set_value;
        current_time.sec = current_time.sec_set_value;
        printf("Set time: %d:%d:%d\n", current_time.hour, current_time.min, current_time.sec);   
    }
    else if (obj == btn_no) {
        //取消日期和时间设置
        current_time.hour_set_value = current_time.hour;
        current_time.min_set_value = current_time.min;
        current_time.sec_set_value = current_time.sec;
        lv_obj_del(currentdate_panle);//关闭设置窗口
        lv_obj_add_flag(Date_Time_set_btn, LV_OBJ_FLAG_CLICKABLE);//允许再次点击
        lv_obj_clear_state(Date_Time_set_btn, LV_STATE_PRESSED);//弹起按钮
        lv_obj_add_state(Date_Time_set_btn, LV_STATE_DEFAULT);//设置按钮为默认状态
        printf("Cancel date and time set\n");
    }
}
/**
 * @brief UI修改标志函数
 * @param target 要修改的对象
 * @param flag 要修改的标志
 * @param value 修改值，0为清除，1为添加，2为切换
 * @param e 
 */
static void _ui_flag_modify(lv_obj_t* target, int32_t flag, int value)
{
    if (value == _UI_MODIFY_FLAG_TOGGLE) {
        if (lv_obj_has_flag(target, flag)) lv_obj_clear_flag(target, flag);
        else lv_obj_add_flag(target, flag);
    }
    else if (value == _UI_MODIFY_FLAG_ADD) lv_obj_add_flag(target, flag);
    else lv_obj_clear_flag(target, flag);
}

/**
 * @brief 字符串分解函数
 * @param str 要分解的字符串
 * @param output 分解出的字符串数组
 * @param num_pairs 分解出的字符串个数
 * @param e
 */
static void split_string(const char* str, char output[][PAIR_LEN + 1], int num_pairs) {//PAIR_LEN + 1是为了每组分解出的字符串后面都加\0
    for (int i = 0; i < num_pairs; i++) {
        // 使用strncpy来复制子字符串，并确保不会超出目标数组的边界  
        strncpy(output[i], str + i * PAIR_LEN, PAIR_LEN);
        // 添加字符串结束符'\0'（strncpy通常已经做了这件事，但为了确保，我们可以再次设置）  
        output[i][PAIR_LEN] = '\0';
    }
}
